package online.hxstart.code.java8.chapter05;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * @author huxing
 * @date 2019/6/21 星期五 19:30
 * @describe
 */
public class Test {

  static List<Translation> list = Arrays.asList(
      new Translation("qewq", "milan", 5000, 2011),
      new Translation("huewqsd", "milan", 5100, 2011),
      new Translation("ewqhuasd", "jq", 5030, 2011),
      new Translation("qehuasd", "milan", 5200, 2011),
      new Translation("eqhuasd", "milan", 5400, 2011),
      new Translation("ewqqhuasd", "hilan", 5500, 2011),
      new Translation("qqqhuasd", "hilan", 5060, 2021),
      new Translation("eqhuasd", "dslan", 5010, 2031),
      new Translation("eqhuasd", "mslan", 5100, 2021),
      new Translation("dshuasd", "jq", 5200, 2031),
      new Translation("fdshuasd", "milfdsan", 5700, 2011),
      new Translation("gfdhuasd", "milafn", 5800, 2011),
      new Translation("jhghuasd", "milanf", 5400, 2041),
      new Translation("gdhuasd", "milan", 5010, 2021),
      new Translation("kjkhuasd", "jq", 2560, 2041),
      new Translation("eqhuasd", "damilan", 5003, 2011),
      new Translation("jhgghuasd", "dasdmilan", 3000, 2031),
      new Translation("ouuhuasd", "jq", 4000, 2021),
      new Translation("tyrhuasd", "dasmilan", 5000, 2011),
      new Translation("uyrhuasd", "jq", 6200, 2021),
      new Translation("uytuhuasd", "jq", 5000, 2011)
  );

  public static void main(String[] args) {
    //
    System.out.println(list.stream().filter(a -> a.getYear() == 2011)
        .sorted(Comparator.comparingInt(Translation::getMoneyNum)).collect(Collectors.toList()));
    System.out.println(
        list.stream().map(Translation::getAddress).distinct().collect(Collectors.toList()));

    System.out.println(list.stream().filter(translation -> "jq".equals(translation.getAddress()))
        .sorted(Comparator.comparing(Translation::getName)).collect(Collectors.toList()));

    System.out.println(
        list.stream().map(Translation::getName).map(a -> a.split("")).flatMap(Arrays::stream)
            .sorted(String::compareTo).collect(Collectors.toList()));
    System.out.println(list.stream().anyMatch(t -> "ml".equals(t.getAddress())));
    System.out.println(
        list.stream().filter(t -> "jq".equals(t.getAddress())).collect(Collectors.toList()));

    System.out.println(
        list.stream().filter(t -> "jq".equals(t.getAddress())).map(Translation::getMoneyNum)
            .collect(Collectors.toList()));
    System.out.println(list.stream().map(Translation::getMoneyNum).reduce(Integer::max));
//    System.out.println(list.stream().reduce());

  }
}

@Data
@AllArgsConstructor
class Translation {

  private String name;
  private String address;
  private int moneyNum;
  private int year;
}
